package com.rongke.web.thirdapi;

import com.alibaba.fastjson.JSONObject;
import com.rongke.model.BankcardInfo;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
*银行卡四要素验证
 */
public class BankCard {

    public static void main(String[] args) {
        BankcardInfo bankcardInfo = new BankcardInfo();
        bankcardInfo.setRealName("");
        bankcardInfo.setIdCard("");
        bankcardInfo.setBankNum("");
        BankCard bankCard = new BankCard();
        boolean b = bankCard.checkBankCard(bankcardInfo, "");
        System.out.println(b);
    }
    public boolean  checkBankCard(BankcardInfo bankcardInfo,String phone) {
        String url="http://api.chinadatapay.com/communication/personal/1887";
        ThirdApiUtil thirdApiUtil = new ThirdApiUtil();
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("key", "b579c429d608d9b88d556e2218afd46a"));
        nvps.add(new BasicNameValuePair("name", bankcardInfo.getRealName()));
        nvps.add(new BasicNameValuePair("idcard", bankcardInfo.getIdCard()));
        nvps.add(new BasicNameValuePair("acc_no", bankcardInfo.getBankNum()));//
        nvps.add(new BasicNameValuePair("mobile", phone ));
        String post=thirdApiUtil.httpPost(url,nvps);  //post请求
        System.out.println("1111"+post);
        JSONObject jsonObject = JSONObject.parseObject(post);
        String code = jsonObject.getString("code");
        if ("1000".equals(code)){
            JSONObject data = jsonObject.getJSONObject("data");
            Integer state = data.getInteger("state");

            if (state==1){
                return true;
            }
        }

        return false;
/*        String getparam="userCode=用户名&userPass=密码&DesNo=手机号&Msg=短信内容【签名】&Channel=通道号";
        String result=httpGet(url,getparam); //get请求*/
    }

    public  void setBankcardInfo(BankcardInfo bankcardInfo) {
        String url="https://ccdcapi.alipay.com/validateAndCacheCardInfo.json";
        ThirdApiUtil thirdApiUtil = new ThirdApiUtil();
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("cardNo",bankcardInfo.getBankNum()));
        nvps.add(new BasicNameValuePair("cardBinCheck","true"));
        String s = thirdApiUtil.httpPost(url, nvps);
        JSONObject jsonObject = JSONObject.parseObject(s);
        String bank = jsonObject.getString("bank");
        String cardType = jsonObject.getString("cardType");
        bankcardInfo.setBankName(bankMap.get(bank));
        bankcardInfo.setType(bankTypeMap.get(cardType));

//        bankcardInfo.setBankName();
        System.out.println("银行："+bank+"...."+cardType);
    }

    Map<String,String> bankMap = new HashMap<>();
    {
        bankMap.put("HZCB","杭州银行");
        bankMap.put("ABC","农业银行");
        bankMap.put("BOC","中国银行");
        bankMap.put("ICBC","工商银行");
        bankMap.put("CCB","建设银行");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");
        bankMap.put("","");


    }
    Map<String,Integer> bankTypeMap= new HashMap<>();
    {
        bankTypeMap.put("DC",0);
        bankTypeMap.put("CC",1);

    }

}
